//
// Created by 张珺超 on 2019/10/17.
//

#include "ConsoleColorizer.h"
#include "string_tools.h"

WARLOCK_NS_BEGIN
std::string ConsoleColorizer::reset() const {
  return CSI_HEAD "0" CSI_TAIL;
}

std::string ConsoleColorizer::code(const std::string &codeText) const {
  std::vector<std::string> codes = string_tools::split(codeText, " ");
  std::string result;
  result += CSI_HEAD;
  for (const auto &x : codes) {
    auto search = m_codeMap.find(x);
    if (search != m_codeMap.end()) {
      if (result != CSI_HEAD) result += ";";
      result += search->second;
    }
  }
  result += CSI_TAIL;
  return result;
}

const std::map<std::string, std::string> ConsoleColorizer::m_codeMap = {
  {"bold",             "1"},
  {"blur",             "2"},
  {"italic",           "3"},
  {"under_line",       "4"},
  {"blink_slow",       "5"},
  {"blink_fast",       "6"},
  {"reverse",          "7"},
  {"hide",             "8"},
  {"middle_line",      "9"},
  //foreground colors
  {"black",            "30"},
  {"red",              "31"},
  {"green",            "32"},
  {"yellow",           "33"},
  {"blue",             "34"},
  {"purple",           "35"},
  {"cyan",             "36"},
  {"white",            "37"},
  {"transparent",      "3"},
  {"default",          "39"},
  //background colors
  {"black_back",       "40"},
  {"red_back",         "41"},
  {"green_back",       "42"},
  {"yellow_back",      "43"},
  {"blue_back",        "44"},
  {"purple_back",      "45"},
  {"cyan_back",        "46"},
  {"white_back",       "47"},
  {"default_back",     "49"},
  {"transparent_back", "4"}
};

WARLOCK_NS_END


